import java.io.BufferedReader;
import java.io.FileReader;
import java.util.LinkedList;


public class MapManager {
	String path;
	int mapNumber;
	public final  int SIZE = 100;  //the size(pixles) of a tile
	Collidable[][]map = new Collidable[150][150];
	/**
	 * map manager works by pulling in data from the map txt file.  It only stores what is not a free space.
	 * it does not create the objects the map says to, just stores where they should be.  Then, when the object
	 * is needed, it creates it and adds it to the temporary linked list to be sent out.  For drawing,
	 * it knows the images for walls and all that stuff, so it just sends out the positions of the walls in a
	 * linked list, and has a method for seeing what image they should be displayed as.
	 * 
	 */
	public MapManager()
	{
		path = getClass().getClassLoader().getResource(".").getPath()+ "\\Maps\\Map";
		mapNumber = 0;
		loadMap(0);
	}
	public void loadMap(int num)
	{
		FileReader fr;
		try {
			fr = new FileReader(path + num + ".txt");

			BufferedReader br = new BufferedReader(fr);
			String str = br.readLine();
			int i = 0;
			while(str != null)
			{
				int j = 0;
				while(j<str.length())
				{
					char val = str.charAt(j);
					map[i][j] = null;
					if(val == 'w')
					{
						map[i][j] = new Wall(new Point(SIZE*j,SIZE*i),SIZE/2);
					}
					j++;
				}
				str = br.readLine();
				i++;
			}
			fr.close();
			br.close();
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}
	/**
	 * gets the object that the character is running into.
	 * @param p
	 * @return
	 */
	public LinkedList<Collidable> getCollidable(Point p)
	{
		try{
		LinkedList<Collidable> ans = new LinkedList<Collidable>();
		int x = (int)(p.getX()/SIZE);
		int y = (int)(p.getY()/SIZE);
		int i = -1;
		while(i <=1)
		{
			int j = -1;
			while(j<=1)
			{
				if(x+j >=0 && y+i >= 0)
				{
					if(map[y+i][x+j] != null)
					{
						ans.add(map[y+i][x+j]);
					}
				}
				j++;
			}
			i++;
		}
		return ans;
		}
		catch(Exception e)
		{
			return null;
		}
	}
	public String getDrawable()
	{
		return "";
	}

}
